package com.wsllx.service;

import com.wsllx.spring.BeanPostProcessor;
import com.wsllx.spring.Component;

import java.lang.reflect.Proxy;

/**
 * @Classname LcBeanPostProcsssor
 * @Description TODO
 * @Date 2022/4/7 10:37
 * @Created by xinzong
 */
@Component
public class LcBeanPostProcsssor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if (beanName != null && beanName.equals("userService")) {
            System.out.println("前置······LcBeanPostProcsssor.postProcessBeforeInitialization");
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if (beanName != null && beanName.equals("userInterfaceImpl")) {
            // jdk动态代理
            Object instance = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(),
                    (proxy, method, args) -> {
                        System.out.println("切面逻辑······");
                        return method.invoke(bean, args);
                    });
            return instance;
        }
        return bean;
    }
}
